const app = getApp()

Page({
  data: {
    name1: '张',
    name2: '三',
    sysWidth: 0,
    share: false,
    list: []
  },

  onLoad: function () {
    var that = this
    wx.request({
      url: app.globalData.url + 'cmcxs/config/v/' + app.globalData.version,
      method: 'GET',
      success: (res) => {
        app.globalData.share = res.data.data.share
        app.globalData.bannerlist = res.data.data.list
        that.setData({
          sysWidth: app.globalData.systemInfo.windowWidth,
          share: app.globalData.share,
          list: app.globalData.bannerlist
        })
      }
    })
  },

  onShareAppMessage: function () {
    return {
      title: '快来看看全国有多少人跟你重名',
      imageUrl: '/images/share.jpg',
      path: '/pages/index/index'
    }
  },

  formSubmit: function (e) {
    var that = this
    var str_name1 = e.detail.value.input_name1 + ''
    var str_name2 = e.detail.value.input_name2 + ''
    if (e.detail.value.input_name1 && e.detail.value.input_name2) {
      if (e.detail.value.input_name1.length > 2) {
        wx.showModal({
          title: '- 温馨小提示 -',
          content: '姓氏最大最能输入两个字哦~',
          showCancel: false,
          cancelText: '取消',
          cancelColor: '#000000',
          confirmText: '朕知道了',
          confirmColor: '#3CC51F',
        });
        return
      } else if (e.detail.value.input_name1.length > 3) {
        wx.showModal({
          title: '- 温馨小提示 -',
          content: '名字最大最能输入三个字哦~',
          showCancel: false,
          cancelText: '取消',
          cancelColor: '#000000',
          confirmText: '朕知道了',
          confirmColor: '#3CC51F',
        });
        return
      }
      var pattern = /^[\u4E00-\u9FA5]{1,5}$/;
      let test1 = pattern.test(str_name1)
      let test2 = pattern.test(str_name2)
      if (test1 && test2) {
        that.setData({
          name1: e.detail.value.input_name1,
          name2: e.detail.value.input_name2
        })
      } else {
        wx.showModal({
          title: '- 温馨小提示 -',
          content: '只能输入中文哦~',
          showCancel: false,
          cancelText: '取消',
          cancelColor: '#000000',
          confirmText: '朕知道了',
          confirmColor: '#3CC51F',
        });
        return
      }
    } else {
      wx.showModal({
        title: '- 温馨小提示 -',
        content: '姓氏和名字不能为空哟~',
        showCancel: false,
        cancelText: '取消',
        cancelColor: '#000000',
        confirmText: '朕知道了',
        confirmColor: '#3CC51F',
      });
      return
    }
    wx.request({
      url: app.globalData.url + 'cmcxs/query',
      data: {
        surname: that.data.name1,
        name: that.data.name2,
      },
      method: 'POST',
      success: (res) => {
        wx.navigateTo({
          url: '/pages/result/result?data=' + JSON.stringify(res.data.data) + '&name=' + that.data.name
        });
      },
      fail: () => { },
    });
  },

  toMiniProgress: function (e) {
    let appid = e.currentTarget.dataset.appid
    wx.navigateToMiniProgram({
      appId: appid,
      path: 'pages/index/index'
    })
  }
})